<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Uploader Queue Tests</title>
<script type="text/javascript" src="../../../../build/yui/yui.js"></script>
<style>
.testclass {
    color: #ff0000;
}
</style>
</head>

<body class="yui3-skin-sam">
<h1>Uploader Queue Tests</h1>
<p><input type="button" value="Run Tests" id="btnRun" disabled=true></p>

<script type="text/javascript">

(function() {

    YUI({
        coverage: ['uploader-queue', 'uploader-flash'],
        filter: (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'min'
    }).use("console", "test", "dump", "uploader-flash", "uploader-queue", function(Y) {



        // Set up the page
        var ASSERT = Y.Assert,
            ARRAYASSERT = Y.ArrayAssert,
            btnRun = Y.one("#btnRun"),
            myConsole = new Y.Console().render();

        btnRun.set("disabled", false);
        Y.on("click", function(){
            Y.Test.Runner.run();
        }, btnRun);


        var BasicQueue = new Y.Test.Case({
            name: "Basic Uploader Queue Tests",

            setUp : function () {
                this.filearray = [];

                for (var i = 0; i <=2; i++) {
                    this.filearray.push(new Y.FileFlash({
                        name: i + ".jpg",
                        size: i * 1000,
                        type: "jpg"
                    }))
                }

                this.basicqueue = new Y.Uploader.Queue({
                    simUploads: 3,
                    errorAction: Y.Uploader.Queue.RESTART_ASAP,
                    fileFieldName: "Filedata",
                    fileList: this.filearray,
                    uploadURL: "test.html",
                    perFileParameters: {}
                })
            },

            tearDown : function () {
                delete this.basicqueue;
                delete this.filearray;
            },


            testQueueBasic : function() {
                this.basicqueue.set("simUploads", 4);
                Y.Assert.areEqual(this.basicqueue.get('simUploads'), 4);

                this.basicqueue._uploadStartHandler({target: {}});

                this.basicqueue._uploadErrorHandler({target: this.filearray[0]});

                this.basicqueue.set("errorAction", Y.Uploader.Queue.RESTART_AFTER);
                this.basicqueue._uploadErrorHandler({target: this.filearray[1]});

                this.basicqueue.set("errorAction", Y.Uploader.Queue.STOP);
                this.basicqueue._uploadErrorHandler({target: this.filearray[2]});
            },

            testQueueProgress : function() {
                this.basicqueue.set("simUploads", 1);
                this.basicqueue.startUpload();

                this.filearray[0].fire("uploadprogress", {originEvent: {},
                                                          bytesLoaded: 50,
                                                          bytesTotal: 100,
                                                          percentLoaded: 50
                                             });
                this.filearray[0].fire("uploadcomplete", {originEvent: {},
                                                          data: "data"});

                this.filearray[1].fire("uploadprogress", {originEvent: {},
                                                          bytesLoaded: 50,
                                                          bytesTotal: 100,
                                                          percentLoaded: 50
                                             });
                this.filearray[1].fire("uploadcomplete", {originEvent: {},
                                                          data: "data"});

                this.filearray[2].fire("uploadprogress", {originEvent: {},
                                                          bytesLoaded: 50,
                                                          bytesTotal: 100,
                                                          percentLoaded: 50
                                             });
                this.filearray[2].fire("uploadcomplete", {originEvent: {},
                                                          data: "data"});
            },

            testQueueMethods : function() {
                this.basicqueue.set("simUploads", 1);
                this.basicqueue.startUpload();

                this.filearray[0].fire("uploadprogress", {originEvent: {},
                                                          bytesLoaded: 50,
                                                          bytesTotal: 100,
                                                          percentLoaded: 50
                                             });

                this.basicqueue.pauseUpload();

                this.filearray[0].fire("uploadcomplete", {originEvent: {},
                                                          data: "data"});

                this.basicqueue.restartUpload();

                this.filearray[1].fire("uploadprogress", {originEvent: {},
                                                          bytesLoaded: 50,
                                                          bytesTotal: 100,
                                                          percentLoaded: 50
                                             });

                this.basicqueue.forceReupload(this.filearray[1]);

                this.filearray[1].fire("uploadcomplete", {originEvent: {},
                                                          data: "data"});

                this.filearray[2].fire("uploadprogress", {originEvent: {},
                                                          bytesLoaded: 50,
                                                          bytesTotal: 100,
                                                          percentLoaded: 50
                                             });
                this.filearray[2].fire("uploadcomplete", {originEvent: {},
                                                          data: "data"});
            },

            testCancelActiveFile : function() {
                this.basicqueue.set("simUploads", 1);
                this.basicqueue.startUpload();

                this.filearray[0].fire("uploadprogress", {originEvent: {},
                                                          bytesLoaded: 50,
                                                          bytesTotal: 100,
                                                          percentLoaded: 50
                                             });

                this.basicqueue.cancelUpload(this.filearray[0]);
            },

            testCancelDoneFile : function() {
                this.basicqueue.set("simUploads", 1);
                this.basicqueue.startUpload();

                this.filearray[0].fire("uploadprogress", {originEvent: {},
                                                          bytesLoaded: 100,
                                                          bytesTotal: 100,
                                                          percentLoaded: 100
                                             });

                this.basicqueue._uploadCompleteHandler({target: this.filearray[0]});

                this.basicqueue.cancelUpload(this.filearray[0]);
            },

            testCancelQueuedFile : function() {
                this.basicqueue.set("simUploads", 1);
                this.basicqueue.startUpload();

                this.filearray[0].fire("uploadprogress", {originEvent: {},
                                                          bytesLoaded: 50,
                                                          bytesTotal: 100,
                                                          percentLoaded: 50
                                             });

                this.basicqueue.cancelUpload(this.filearray[1]);
            },


            testCancel : function() {
                this.basicqueue.set("simUploads", 1);
                this.basicqueue.startUpload();

                this.filearray[0].fire("uploadprogress", {originEvent: {},
                                                          bytesLoaded: 50,
                                                          bytesTotal: 100,
                                                          percentLoaded: 50
                                             });

                this.basicqueue.cancelUpload();
            },

            testAddQueue : function () {
                this.basicqueue.addToQueueBottom(this.filearray[3]);
                this.basicqueue.addToQueueTop(this.filearray[2]);
            }
        });

        var suite = new Y.Test.Suite("Uploader: Queue");
        suite.add(BasicQueue);

        Y.Test.Runner.add(suite);
        Y.Test.Runner.run();
    });

})();
</script>
</body>
</html>
